package com.dkts.oil.util;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author lgd liguodong@itbox.cn
 * @date 2024-05-15
 * @description: 集合工具类
 * @version: 1.0.0
 */
public class ListUtil {
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    /**
     * 合并两个Map
     * @param map1  Map1
     * @param map2  Map2
     * @return 合并后的Map
     */
    public static Map<String, Integer> mergeMaps(Map<String, Integer> map1, Map<String, Integer> map2) {
        Map<String, Integer> mergedMap = new HashMap<>(20);

        for (String key : map1.keySet()) {
            if (map2.containsKey(key)) {
                int sum = map1.get(key) + map2.get(key);
                mergedMap.put(key, sum);
            }
        }
        return mergedMap;
    }

    @SuppressWarnings("unchecked")
    public static <T> List<Map<String, Object>> convertBeanListToMapList(List<T> beanList) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (T bean : beanList) {
            mapList.add(OBJECT_MAPPER.convertValue(bean, Map.class));
        }
        return mapList;
    }
}
